package saleMarket.service;

import saleMarket.dao.InventoryGoodsDao;
import saleMarket.domain.InventoryGoods;
import util.JdbcHelper;
import util.exception.ErrorException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

/**
 * @author 张方恒
 * @version 1.0  12-13-2020
 */

public class InventoryGoodsService {
    private static InventoryGoodsDao inventoryGoodsDao = InventoryGoodsDao.getInstance();
    private static InventoryGoodsService inventoryGoodsService =new InventoryGoodsService();
    private InventoryGoodsService(){}

    public static InventoryGoodsService getInstance(){
        return inventoryGoodsService;
    }

    public Collection<InventoryGoods> findAll() throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Collection<InventoryGoods> inventoryGoods = null;
        try{
            inventoryGoods = inventoryGoodsDao.findAll(connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return inventoryGoods;
    }

    public InventoryGoods find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        InventoryGoods inventoryGoods = inventoryGoodsDao.find(id,connection);
        System.out.println(inventoryGoods);
        JdbcHelper.close(connection);
        return inventoryGoods;
    }

    public boolean update(InventoryGoods inventoryGoods) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean updated = false;
        try{
            updated = inventoryGoodsDao.update(inventoryGoods,connection);
            connection.commit();
        }catch (SQLException e){
            System.out.println(e);
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(InventoryGoods inventoryGoods) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean added = false;
        try{
            added = inventoryGoodsDao.add(inventoryGoods,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }
    public boolean delete(Integer id) throws SQLException, ErrorException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean deleted = false;
        try{
            deleted = inventoryGoodsDao.delete(id, connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }

        return deleted;
    }

    public boolean buy(InventoryGoods inventoryGoods)throws SQLException,ErrorException{
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean buy = false;
        try{
            if(inventoryGoods.getStock()>=0){
                buy = inventoryGoodsDao.buy(inventoryGoods,connection);
                System.out.println(buy);
            }else{
                throw new ErrorException("库存不足");
            }
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return buy;
    }


}
